﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBUtility;
using CUREEntity;
using System.Data;

/*************************************************************************************

     * 作   者： 张 颖
  
*************************************************************************************/
namespace CUREDataAccessLayers
{
    public class ModuleDal
    {
        /// <summary>
        /// 获取主模块数据
        /// </summary>
        /// <returns>返回表</returns>
        public DataTable GetActionColumnData()
        {
            DataTable dt = new DataTable();
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select c.action_columnid, c.action_columnname from actioncolumn c order by c.action_colsort asc");
                dt = DbHelperOra.QueryTable(commandText.ToString());
                //this.DbHelperInstance.Fill(dt, commandText);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        /// <summary>
        /// 根据id获取从模块数据
        /// </summary>
        /// <returns>返回表</returns>
        public DataTable GetActionData(string id)
        {
            DataTable dt = new DataTable();
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select a.action_id, a.action_name,action,a.action_columnid from action a where a.action_columnid =" + id + " order by a.action_sort");
                dt = DbHelperOra.QueryTable(commandText.ToString());
                //this.DbHelperInstance.Fill(dt, commandText);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        /// <summary>
        /// 根据父节点编号获取子节点信息
        /// </summary>
        /// <returns></returns>
        /// 
        public DataTable GetChildModuleByfatherId(string fatherid)
        {
            DataTable dt = new DataTable();
            StringBuilder sql = new StringBuilder();
            sql.Append("select t.action_columnname as ACTION_COLUMNNAME,a.action_name as ACTION_NAME ,a.action  as ACTION ,a.action_id as ACTION_ID,a.ACTION_SORT from actioncolumn t,action a where a.action_columnid=t.action_columnid and t.action_columnid='" + fatherid + "' order by a.action_sort");
            try
            {
                dt = DbHelperOra.QueryTable(sql.ToString());
                //this.DbHelperInstance.Fill(dt, sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        /// <summary>
        /// 根据子节点获取子节点
        /// </summary>
        /// <returns></returns>
        /// 
        public DataTable GetChildModuleById(string chindname)
        {
            DataTable dt = new DataTable();
            StringBuilder sql = new StringBuilder();
            sql.Append("select t.action_columnname as ACTION_COLUMNNAME,a.action_name as ACTION_NAME ,a.action as ACTION ,a.action_id as ACTION_ID,a.ACTION_SORT from actioncolumn t,action a where a.action_columnid=t.action_columnid and a.action='" + chindname + "' order by a.action_sort");
            try
            {
                dt = DbHelperOra.QueryTable(sql.ToString());
               // this.DbHelperInstance.Fill(dt, sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        /// <summary>
        /// 添加模块
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="url"></param>
        /// <param name="father"></param>
        /// <returns></returns>
        public int InsertModel(string id, string name, string url, string father, string actionSort)
        {
            int result = 0;
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("insert into action(action_id,action_name,action,action_columnid,action_sort)values('" + id + "','" + name.Trim() + "','" + url.Trim() + "','" + father.Trim() + "','" + actionSort + "')");

                result = DbHelperOra.ExecuteSql(commandText.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        /// <summary>
        /// 更新模块
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="url"></param>
        /// <param name="father"></param>
        /// <returns></returns>
        public int UpdateModel(string id, string name, string url, string father, string actionSort)
        {
            int result = 0;
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("update action set action_name='" + name.Trim() + "',action='" + url.Trim() + "',action_columnid='" + father.Trim() + "',action_sort='"+ actionSort+"' where action_id='" + id + "'");
                result = DbHelperOra.ExecuteSql(commandText.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        /// <summary>
        /// 删除模块
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public int DeleteModel(string url)
        {
            int result = 0;
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("delete from action where action='" + url.Trim() + "'");
                result = DbHelperOra.ExecuteSql(commandText.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        /// <summary>
        /// 获取ID
        /// </summary>
        /// <returns></returns>
        public decimal getMaxId()
        {
            StringBuilder sql = new StringBuilder();
            decimal actionId = 0;
            try
            {
                sql.Append("select max(t.action_id) from action t ");

                actionId = DbHelperOra.GetMaxID("action_id", "action");
            }
            catch(Exception ex)
            {
                throw ex;
            }
            return actionId;
        }
        /// <summary>
        /// 根据ur id 获取模块信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public DataTable GetModelByurl(string id, string url)
        {
            DataTable dt = new DataTable();
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select t.action_name,t.action from action t where t.action='" + url.Trim() + "' ");
                if (!string.IsNullOrEmpty(id))
                {
                    commandText.Append(" and t.action_id !='" + id + "'");
                }
                commandText.Append(" order by t.action_sort");
                dt = DbHelperOra.QueryTable(commandText.ToString());
              //  this.DbHelperInstance.Fill(dt, commandText);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        /// <summary>
        /// 获取上级模块名称
        /// </summary>
        /// <returns></returns>
        public DataTable GetFatherModel()
        {
            DataTable dt = new DataTable();
            StringBuilder commaText = new StringBuilder();
            try
            {
                commaText.Append("select t.action_columnid,t.action_columnname from actioncolumn t ");
                commaText.Append(" order by t.action_colsort");
                dt = DbHelperOra.QueryTable(commaText.ToString());
                //this.DbHelperInstance.Fill(dt, commaText);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
        /// <summary>
        /// 根据id获取模块
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DataTable GetModelById(string id)
        {
            DataTable dt = new DataTable();
            StringBuilder commandText = new StringBuilder();
            try
            {
                commandText.Append("select t.action_id as ACTION_ID,");
                commandText.Append(" t.action_name,");
                commandText.Append(" t.action,");
                commandText.Append(" t.action_columnid,");
                commandText.Append(" t.action_sort,");
                commandText.Append(" a.action_columnname");
                commandText.Append(" from action t, actioncolumn a");
                commandText.Append(" where t.action_columnid = a.action_columnid");
                commandText.Append(" and action_id='" + id.Trim() + "'");
                dt = DbHelperOra.QueryTable(commandText.ToString());
                //this.DbHelperInstance.Fill(dt, commandText);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
    }
}
